<?php
class userActions extends framework {
    public $m;
    public $uid;
    
    public function __construct() {
        session_start ();
        if (! key_exists ( 'islogin', $_SESSION ) and ! in_array ( self::$action, array ('login', 'reg', 'binfo' ) )) {
            if (self::get ( 'ajax' ) == '1') {
                die ( json_encode ( array ('result' => 0, 'msg' => '请登录！' ) ) );
            }
            framework::location ( HIGHPHP_WWW_HOST . 'user/login/' );
        }
        if (key_exists ( 'user', $_SESSION )) {
            $this->uid = $_SESSION ['user'] ['uid'];
            self::assign ( 'user', $_SESSION ['user'] );
        }
        framework::model ( 'user' );
        $this->m = new User ();
        framework::addJs ( HIGHPHP_WWW_HOST . 'resource/js/jquery/jquery.js' );
        framework::addCss ( HIGHPHP_WWW_HOST . 'resource/css/frontpage.css' );
        self::assign ( 'keywords', '' );
        self::assign ( 'description', '' );
        self::assign ( 'title', '' );
    }
    public function actionsIndex() {
    
    }
    public function actionsLogin() {
        $ischeckcode = 1;
        if (self::post ()) {
            $username = self::post ( 'username' );
            $pwd = self::post ( 'pwd' );
            $rs = '登录失败';
            $info = '';
            if ($ischeckcode) {
                $checkcode = self::post ( 'checkcode' );
                if (strtolower ( $checkcode ) != strtolower ( @$_SESSION ['captcha'] )) {
                    $rs = '验证码错误！';
                }
            }
            $email = filter_input ( INPUT_POST, 'username', FILTER_VALIDATE_EMAIL );
            
            if ($email) {
                $info = $this->m->getBasicUserByEmail ( $email );
                if (! $info) {
                    $rs = '帐号或密码错误';
                }
            } else {
                framework::plugin ( 'strings' );
                if (strings::checkPhone ( $username ) and strlen ( $username ) == 11) {
                    $info = $this->m->getBasicUserByPhone ( $username );
                    if (! $info) {
                        $rs = '帐号或密码错误';
                    }
                } else {
                    $rs = '帐号格式错误';
                }
            }
            //print_r($info);   
            if ($info) {
                if ($info ['pwd'] != md5 ( $pwd . 'z' )) {
                    $rs = '帐号或密码错误';
                } else {
                    $this->m->setLogoin ( $info ['uid'] );
                    die ( '成功' );
                }
            }
            die ( $rs );
        }
        if (preg_match ( '/user\//', HIGHPHP_PRE_PAGE )) {
            $loginurl = HIGHPHP_PRE_PAGE;
        } else {
            $loginurl = HIGHPHP_WWW_HOST . 'user';
        }
        self::assign ( 'ischeckcode', $ischeckcode );
        self::assign ( 'tourl', $loginurl );
    }
    public function actionsReg() {
        $ischeckcode = 1;
        if (self::post ()) {
            $username = self::post ( 'username' );
            $pwd = self::post ( 'pwd' );
            $phone = self::post ( 'phone' );
            $name = self::post ( 'name' );
            $code = self::post ( 'code' );
            $rs = '注册失败';
            $email = self::post ( 'email' );
            if ($email) {
                $email = filter_input ( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
                if (! $email) {
                    die ( '邮箱格式错误！' );
                }
            }
            
            if ($phone) {
                if (! checkPhone ( $phone )) {
                    die ( '请填写正确的手机号！' );
                }
            }
            if (! $email and ! $phone) {
                die ( '请选择邮箱或手机进行注册！' );
            }
            if (strlen ( $pwd ) > 16 or strlen ( $pwd ) < 6) {
                die ( '密码长度需大于6位小于16位！' );
            }
            if (strlen ( $username ) > 16 or strlen ( $username ) < 3) {
                die ( '用户名长度需大于3位小于16位' );
            }
            if (strlen ( $name ) > 16 or strlen ( $name ) < 6) {
                die ( '姓名长度需大于2位小于16位' );
            }
            $ccode = idcard_checksum18 ( $code );
            if ($ccode !== true) {
                die ( $ccode );
            }
            if ($ischeckcode) {
                $checkcode = self::post ( 'checkcode' );
                if (strtolower ( $checkcode ) != strtolower ( @$_SESSION ['captcha'] )) {
                    die ( '验证码错误！' );
                }
            }
            
            if ($this->m->getBasicUserByEmail ( $email )) {
                die ( '您的邮箱已经注册' );
            }
            if ($this->m->getBasicUserByUsername ( $username )) {
                die ( '您的昵称已经注册' );
            }
            if ($this->m->getBasicUserByCode ( $code )) {
                die ( '您的身份证已经注册' );
            }
            $data = array ('username' => $username, 'email' => $email, 'phone' => $phone, 'pwd' => md5 ( $pwd . 'z' ), 'regtime' => date ( "Y-m-d H:i:s" ), 'name' => $name, 'code' => $code );
            $uid = $this->m->insertUser ( $data );
            if ($uid) {
                $this->m->setLogoin ( $uid );
                die ( '成功' );
            }
            die ( $rs );
        }
        self::assign ( 'ischeckcode', $ischeckcode );
        self::assign ( 'tourl', HIGHPHP_WWW_HOST . 'user' );
    }
    public function actionsLogout() {
        session_destroy ();
        if (HIGHPHP_PRE_PAGE) {
            $this->tourl ( HIGHPHP_PRE_PAGE );
        } else {
            $this->tourl ( HIGHPHP_WWW_HOST );
        }
    }
    public function actionsBinfo() {
        $jsoncallback = self::get ( 'jsoncallback' );
        $data = array ('result' => 0 );
        if (key_exists ( 'islogin', $_SESSION ) and $jsoncallback) {
            $uid = $_SESSION ['user'] ['uid'];
            $data = $this->m->getBasicUserByUid ( $uid );
            $data ['result'] = 1;
        }
        
        die ( $jsoncallback . '(' . json_encode ( $data ) . ')' );
    }
    public function actionsSaveuserinfo() {
        if (self::post ()) {
            $username = self::post ( 'username' );
            $pwd = self::post ( 'pwd' );
            $phone = self::post ( 'phone' );
            $name = self::post ( 'name' );
            $code = self::post ( 'code' );
            $rs = '注册失败';
            $email = self::post ( 'email' );
            if ($email) {
                $email = filter_input ( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
                if (! $email) {
                    die ( '邮箱格式错误！' );
                }
            }
            
            if ($phone) {
                if (! checkPhone ( $phone )) {
                    die ( '请填写正确的手机号！' );
                }
            }
            if (! $email and ! $phone) {
                die ( '请选择邮箱或手机进行注册！' );
            }
            if ($pwd) {
                if (strlen ( $pwd ) > 16 or strlen ( $pwd ) < 6) {
                    die ( '密码长度需大于6位小于16位！' );
                }
                $pwd = md5 ( $pwd . 'z' );
            } else {
                $pwd = $_SESSION ['user'] ['pwd'];
            }
            if (strlen ( $username ) > 16 or strlen ( $username ) < 3) {
                die ( '用户名长度需大于3位小于16位' );
            }
            if (strlen ( $name ) > 16 or strlen ( $name ) < 6) {
                die ( '姓名长度需大于2位小于16位' );
            }
            $ccode = idcard_checksum18 ( $code );
            if ($ccode !== true) {
                die ( $ccode );
            }
            if ($phone and ! $_SESSION ['user'] ['phone']) {
                if ($this->m->getBasicUserByPhone ( $phone )) {
                    die ( '您的邮箱已经注册' );
                }
            }
            if ($email and ! $_SESSION ['user'] ['email']) {
                if ($this->m->getBasicUserByEmail ( $email )) {
                    die ( '您的邮箱已经注册' );
                }
            }
            if ($username and $username != $_SESSION ['user'] ['username']) {
                if ($this->m->getBasicUserByUsername ( $username )) {
                    die ( '您的昵称已经注册' );
                }
            }
            if ($code and $code != $_SESSION ['user'] ['code']) {
                if ($this->m->getBasicUserByCode ( $code )) {
                    die ( '您的身份证已经注册' );
                }
            }
            $data = array ('username' => $username, 'email' => $email, 'phone' => $phone, 'pwd' => $pwd, 'name' => $name, 'code' => $code );
            $uid = $this->m->saveBasicinfo ( $data, $this->uid );
            if ($uid) {
                $this->m->setLogoin ( $this->uid );
                die ( '成功' );
            }
            die ( $rs );
        }
    }
    public function actionsAddress() {
        $uid = $this->uid;
        if (self::get ( 'do' ) == 'save') {
            $name = self::post ( 'name' );
            $phone = self::post ( 'phone' );
            $tel1 = self::post ( 'tel1' );
            $tel2 = self::post ( 'tel2' );
            $tel3 = self::post ( 'tel3' );
            $areacode = self::post ( 'areacode' );
            $province = self::post ( 'province' );
            $city = self::post ( 'city' );
            $area = self::post ( 'area' );
            $street = self::post ( 'street' );
            $isdefault = self::post ( 'isdefault' );
            if (! $name or ! $areacode or ! $province or ! $city or ! $area or ! $street) {
                die ( '填写信息不能为空！' );
            }
            if ($tel1 and ! $tel2) {
                die ( '填写信息不能为空！' );
            }
            if (! $phone and ! $tel1) {
                die ( '填写信息不能为空！' );
            }
            if ($phone) {
                if (! checkPhone ( $phone )) {
                    die ( '请正确填写手机号！' );
                }
            }
            
            if ($isdefault == 'true') {
                $isdefault = 1;
            } else {
                $isdefault = 0;
            }
            if ($this->m->getAddressCount ( $uid ) >= 5) {
                die ( '最多能设置5个地址！' );
            }
            
            $data = array ('uid' => $uid, 'name' => $name, 'phone' => $phone, 'tel1' => $tel1, 'tel2' => $tel2, 'tel3' => $tel3, 'areacode' => $areacode, 'province' => $province, 'city' => $city, 'area' => $area, 'street' => $street, 'isdefault' => $isdefault );
            $id = $this->m->db->insert ( 'user_addr', $data, true );
            if ($isdefault)
                $this->actionsSetdeaultaddr ( $id );
            echo '添加成功！';
            die ();
        }
        framework::addJs ( HIGHPHP_WWW_HOST . 'resource/js/system/jsAddress.js' );
        $rs = $this->m->getAddress ( $this->uid );
        self::assign ( 'rs', $rs );
    
    }
    public function actionsSetdeaultaddr($id = null) {
        $uid = $this->uid;
        $aid = self::get ( 'id', $id );
        if (! $aid) {
            die ( '请设置id' );
        }
        $data = array ('isdefault' => 0 );
        $this->m->db->updateData ( 'user_addr', $data, " WHERE uid = '$uid'" );
        $data = array ('isdefault' => 1 );
        $this->m->db->updateData ( 'user_addr', $data, " WHERE id='$aid' AND uid = '$uid'" );
        if (! $id) {
            die ( '设置成功' );
        } else {
            return true;
        }
    }
    public function actionsBank() {
        $uid = $this->uid;
        $row = array ();
        if (self::get ( 'do' ) == 'save') {
            $id = self::get ( 'id' );
            $name = self::post ( 'name' );
            $bankname = self::post ( 'bankname' );
            $branch = self::post ( 'branch' );
            $bankno = self::post ( 'bankno' );
            $phone = self::post ( 'phone' );
            $province = '';
            $city = '';
            $area = '';
            if ($bankname != '支付宝') {
                $province = self::post ( 'province' );
                $city = self::post ( 'city' );
                $area = self::post ( 'area' );
            }
            $isdefault = self::post ( 'isdefault' );
            if (! $name or ! $bankname or ! $bankno) {
                die ( '填写信息不能为空！' );
            }
            if ($isdefault == 'true') {
                $isdefault = 1;
            } else {
                $isdefault = 0;
            }
            if ($this->m->getBankCount ( $uid ) >= 5) {
                die ( '最多能设置5个收款信息！' );
            }
            $data = array ('uid' => $uid, 'name' => $name, 'province' => $province, 'city' => $city, 'area' => $area, 'bankname' => $bankname, 'branch' => $branch, 'bankno' => $bankno, 'phone' => $phone, 'isdefault' => $isdefault );
            if ($id) {
                $this->m->db->updateData ( 'user_bank', $data, " WHERE id='$id' AND uid = '$uid'" );
            } else {
                $id = $this->m->db->insert ( 'user_bank', $data, true );
            }
            if ($isdefault)
                $this->actionsSetdeaultBank ( $id );
            echo '保存成功！';
            die ();
        } elseif (self::get ( 'do' ) == 'edit') {
            $id = self::get ( 'id' );
            $sql = "SELECT * FROM user_bank WHERE id='$id' AND uid = '$uid' ";
            $row = $this->m->db->getRow ( $sql );
        
        }
        self::assign ( 'row', $row );
        framework::addJs ( HIGHPHP_WWW_HOST . 'resource/js/system/jsAddress.js' );
        $rs = $this->m->getBank ( $this->uid );
        self::assign ( 'rs', $rs );
    }
    public function actionsSetdeaultBank($id = null) {
        $uid = $this->uid;
        $aid = self::get ( 'id', $id );
        if (! $aid) {
            die ( '请设置id' );
        }
        $data = array ('isdefault' => 0 );
        $this->m->db->updateData ( 'user_bank', $data, " WHERE uid = '$uid'" );
        $data = array ('isdefault' => 1 );
        $this->m->db->updateData ( 'user_bank', $data, " WHERE id='$aid' AND uid = '$uid'" );
        if (! $id) {
            die ( '设置成功' );
        } else {
            return true;
        }
    }
    public function actionsPublish() {
        if (self::get ( 'do' ) == 'save') {
            $id = self::get ( 'id' );
            $type = self::post ( 'type' );
            $typeinfo = self::post ( 'typeinfo' );
            $isfree = self::post ( 'isfree' );
            $title = self::post ( 'title' );
            $content = self::post ( 'content' );
            $desc = self::post ( 'desc' );
            $img = self::post ( 'img' );
            $point = self::post ( 'point' );
            
            $t = time ();
            
            if ($type != '0' && $type != 4) {
                if ($typeinfo == '') {
                    echo '正确填写信息！';
                    die ();
                }
            }
            if ($point == '' && $isfree == '0') {
                echo '您选择了共享信息，请设置积分！';
                die ();
            }
            if (! $title) {
                echo '标题必须填写！';
                die ();
            }
            if (! $desc) {
                echo '简介必须填写！';
                die ();
            }
            if (! $content) {
                echo '详细内容！';
                die ();
            }
            
            $data = array ('uid' => $this->uid, 'type' => $type, 'typeinfo' => $typeinfo, 'isfree' => $isfree, 'title' => $title, 'content' => $content, 'desc' => $desc, 'img' => $img, 'point' => $point, 'ctime' => $t, 'utime' => $t );
            if ($id) {
                unset ( $data ['ctime'] );
                if ($this->m->db->updateData ( 'user_publish', $data, " WHERE id='$id' AND uid = '" . $this->uid . "'" )) {
                    echo '修改成功';
                    die ();
                }
            } else {
                if ($this->m->db->insert ( 'user_publish', $data )) {
                    echo '添加成功';
                    die ();
                }
            }
            echo '提交失败';
            die ();
        } elseif (self::get ( 'do' ) == 'list') {
            $rs = $this->m->getUserPublish ( $this->uid );
            self::assign ( 'rs', $rs );
            self::view ( 'publish_list' );
        } elseif (self::get ( 'do' ) == 'edit') {
            
            $rs = $this->m->getUserPublishInfo ( self::get ( 'id' ), $this->uid );
            //print_r($rs);
            self::assign ( 'info', $rs );
        }
    }
    public function actionsLottery() {
        $ar = array (1 => 1, 2 => 4, 3 => 6, 4 => 10, 5 => 15, 6 => 20, 7 => 50, 8 => 100 );
        $ar2 = array (1 => '1分', 2 => '4分', 3 => '6分', 4 => '10分', 5 => '15分', 6 => '20分', 7 => '50分', 8 => '100分' );
        if (self::get ( 'do' ) == 'award') {
            
            $arr = array (1 => 6, //1分
2 => 24, //4分
3 => 30.99, //6分
4 => 10, //10分
5 => 25, //15分
6 => 3, //20分
7 => 1, //50分
8 => 0.01 ); //100分     
            $v = user::pro_rand ( $arr );            
            $p = $ar [$v];            
            $msg = '';
            if ($p > '0')
                $msg = '再接再厉！';
            if ($p >= '10')
                $msg = '你真棒！';
            if ($p >= '20')
                $msg = '你太牛了！';
            if ($p == '50')
                $msg = '真是神了！';
            if ($p == '100')
                $msg = '逆天了！';
            $msg2 = $msg . '你获得了：' . $p . '分！';
            $msg = date ( "H:i:s " ) . '你获得了：<span class="point">' . $p . '</span>分！';
            if (! $this->m->setUserData ( array ('point' => - 10 ) )) {
                echo json_encode ( array ('result' => 0, 'msg' => '你的积分不足，快去赚积分吧' ) );
                die ();
            }
            $data = array ('uid' => $this->uid, 'type' => 5, 'point' => $p, 'expend' => 10, 'timeline' => time (), 'remark' => '幸运转盘' );
            $this->m->db->insert ( 'user_award', $data );
            
            echo json_encode ( array ('result' => 1, 'num' => $v, 'msg' => $msg, 'msg2' => $msg2 ) );
            die ();
        } elseif (self::get ( 'do' ) == 'awardlist') {
            echo json_encode ( array ('result' => 1, 'msg' => '成功', 'list' => $ar2 ) );
            die ();
        } elseif (self::get ( 'do' ) == '24dian') {
            self::view ( 'lottery_24' );
        } elseif (self::get ( 'do' ) == 'list') {
            $rs = $this->m->getLottery ( $this->uid );
            self::assign ( 'rs', $rs );
            self::view ( 'lottery_list' );
        }
        framework::addJs ( HIGHPHP_WWW_HOST . 'resource/js/jquery/swfobject.js' );
    
    }
    public function actionsExchange() {
        if (self::get ( 'do' ) == 'cash') {
            self::view ( 'exchange_cash' );
        }
    }
    public function actionsEarn() {
    
    }
    public function actionsBasicinfo() {
    
    }
    public function actionsChecklogin() {
        echo json_encode ( array ('result' => 1, 'msg' => '成功' ) );
        die ();
    }
}

